package com.helloae.chap06;

import java.io.File;

import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.FeatureClassName;
import com.esri.arcgis.geodatabase.FeatureDataConverter;
import com.esri.arcgis.geodatabase.IFeatureClassName;
import com.esri.arcgis.geodatabase.TableName;
import com.esri.arcgis.geodatabase.Workspace;
import com.esri.arcgis.geodatabase.WorkspaceName;
import com.esri.arcgis.geodatabase.XYEvent2FieldsProperties;
import com.esri.arcgis.geodatabase.XYEventSource;
import com.esri.arcgis.geodatabase.XYEventSourceName;
import com.esri.arcgis.geometry.IProjectedCoordinateSystem;
import com.esri.arcgis.geometry.ProjectedCoordinateSystem;
import com.esri.arcgis.geometry.SpatialReferenceEnvironment;
import com.esri.arcgis.geometry.esriSRProjCSType;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.PropertySet;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 利用XYEventSource将包含x/y坐标的文本文件转换成点shp文件保存。<br>
 * 这里只测试过txt和csv，但我个人觉得后缀名不会影响这个程序。<br>
 * 示例章节号：6.6.1
 * @author zhushijie
 *
 */
public class XYToPoint {

	public static void main(String[] args) throws Exception {
		//初始化AE许可
		EngineInitializer.initializeEngine();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
		
		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		WorkspaceName workspaceName = new WorkspaceName();
		workspaceName.setPathName(ecliseWorkspace+"HelloAE"+File.separator+"DataSet"+File.separator+"chap06");
		workspaceName.setWorkspaceFactoryProgID("esriDataSourcesOleDB.TextFileWorkspaceFactory");
		TableName tableName = new TableName();
		tableName.setName("events.txt");
		tableName.setWorkspaceNameByRef(workspaceName);
		
		//设定匹配字段
		XYEvent2FieldsProperties xyEvent2Fields = new XYEvent2FieldsProperties();
		xyEvent2Fields.setXFieldName("easting");
		xyEvent2Fields.setYFieldName("northing");
		xyEvent2Fields.setZFieldName("");
		
		//指定投影坐标系。
		//如果是地理坐标，也是SpatialReferenceEnvironment
		//但后面需要用IGeographicCoordinateSystem和createGeographicCoordinateSystem()方法
		SpatialReferenceEnvironment spatialReferenceEnvironment = new SpatialReferenceEnvironment();
		IProjectedCoordinateSystem projectedCoordinateSystem = new ProjectedCoordinateSystem();
		projectedCoordinateSystem = spatialReferenceEnvironment.createProjectedCoordinateSystem(esriSRProjCSType.esriSRProjCS_NAD1927UTM_11N);
		
		XYEventSourceName xyEventSourceName = new XYEventSourceName();
		xyEventSourceName.setEventPropertiesByRef(xyEvent2Fields);
		xyEventSourceName.setSpatialReferenceByRef(projectedCoordinateSystem);
		xyEventSourceName.setEventTableNameByRef(tableName);
		
//			//下面可以转成FeatureLayer，比如加到ArcMap里显示用
//			XYEventSource xyEventSource = new XYEventSource(xyEventSourceName.open());
//			FeatureLayer featureLayer = new FeatureLayer();
//			featureLayer.setFeatureClassByRef(xyEventSource);
		
		
		WorkspaceName pOutShWSName = new WorkspaceName();
		pOutShWSName.setPathName("c:\\temp");
		//路径设置既可以使用setPathName，也可是使用.setProperty，比如下面三行代码
//			PropertySet pPropset = new PropertySet();
//			pPropset.setProperty("DATABASE", ecliseWorkspace+"HelloAE"+File.separator+"DataSet");
//			pOutShWSName.setConnectionProperties(pPropset);
		pOutShWSName.setWorkspaceFactoryProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");
		
		FeatureClassName pOutShFCName = new FeatureClassName();
		pOutShFCName.setWorkspaceNameByRef(pOutShWSName);
		//这里的后缀名".shp"可要可不要，不影响程序运行
		pOutShFCName.setName("helloPoint.shp");
		
		FeatureDataConverter featureDataConverter = new FeatureDataConverter();
		featureDataConverter.convertFeatureClass(xyEventSourceName, null, null, pOutShFCName, null, null, "", 1000, 0);
		
		ao.shutdown();
		System.out.println("Congratulations!");
	}

}
